package com.itsm.controller;

import com.alibaba.fastjson.JSONObject;
import com.itsm.entity.Process;
import com.itsm.entity.User;
import com.itsm.service.SendService;
import com.itsm.service.UserService;
import com.itsm.service.mapper.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class SendController {

    @Autowired
    private SendService sendService;

    @Autowired
    private LoginController loginController;
    @Autowired
    private UserService userService;
    @Autowired
    private FileMapper fileMapper;

    @RequestMapping("/sendUser")
    public String sendUser(String code, HttpServletRequest request, RedirectAttributes redirectAttributes) throws Exception {
        String processNo =  request.getParameter("processNo");
        JSONObject jsonObject1 = loginController.getInfo(code);
        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");

        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);

        redirectAttributes.addAttribute("processNo", processNo);

        return "redirect:/oneProcess.html";
    }

    @RequestMapping("/sendUserAlter")
    public String sendUserAlter(String code, HttpServletRequest request, RedirectAttributes redirectAttributes) throws Exception {
        String processNo =  request.getParameter("processNo");
        JSONObject jsonObject1 = loginController.getInfo(code);
        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");

        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);

        redirectAttributes.addAttribute("processNo", processNo);

        return "redirect:/processUpdate.html";
    }

    /**
    @PostMapping("/sendEn")
    public String sendEn(@RequestBody Process process) throws IOException {
        String userId = process.getUserid();
        String processNo = process.getProcessNo();
        //构造消息体
        StringBuffer sb = new StringBuffer();
        String content = "您有新的工单待接收，请点击<a href=\\\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwfbc200b28be3408d&redirect_uri=http://itservice.hmjy-ic.com:8098/getWeUser1&response_type=code&scope=snsapi_privateinfo&state=STATE&agentid=1000023#wechat_redirect&processNo=" +processNo+
                "\\\">工单详情</a>，进行接单或转派";
        sb.append("{");
        sb.append("\"touser\":" + "\"" + userId + "\",");
        sb.append("\"msgtype\":" + "\"" + "text" + "\",");
        sb.append("\"agentid\":" + "\"" + 1000023 + "\",");
        sb.append("\"text\":" + "{");
        sb.append("\"content\":" + "\"" + content + "\"},");
        sb.append("\"safe\":\"0\"");
        sb.append("}");
        //发送消息
        return sendService.sendMessage(sb.toString());
    }
     **/



    @RequestMapping(value = "/isCreate")
    public Object getWeUser(String code, HttpServletRequest request, RedirectAttributes redirectAttributes) throws Exception {
        String processNo =  request.getParameter("processNo");

        JSONObject jsonObject1 = loginController.getInfo(code);

        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");

        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);
        String depart = null;
        String sup = null;
        List<String> supList = JSONObject.parseArray(JSONObject.toJSONString(jsonObject1.get("direct_leader")), String.class);
        if(supList.size()>0){
            sup = supList.get(0);
        }
        List<String> departList = JSONObject.parseArray(JSONObject.toJSONString(jsonObject1.get("department")), String.class);
        if(departList.size()>0){
            depart = departList.get(0);
        }
        User user1 = userService.selectUser(userid);
        if(user1==null){
            User user = new User();
            Integer plant = fileMapper.getPlant(alias);
            user.setName(name);
            user.setWxid(userid);
            user.setPlant(plant);
            user.setDepart(depart);
            user.setSupWxid(sup);
            userService.addUser(user);
        }
        redirectAttributes.addAttribute("processNo", processNo);
//        System.out.println(processNo);

        return "redirect:/oneDesk.html";
    }

    @RequestMapping(value = "/isApprove")
    public Object sendSup(String code, HttpServletRequest request, RedirectAttributes redirectAttributes) throws Exception {
        String processNo =  request.getParameter("processNo");

        JSONObject jsonObject1 = loginController.getInfo(code);

        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");

        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);

        redirectAttributes.addAttribute("processNo", processNo);

        return "redirect:/oneDesk.html";
    }

    @RequestMapping(value = "/isAccept")
    public Object getEn(String code, HttpServletRequest request, RedirectAttributes redirectAttributes) throws Exception {
        String processNo =  request.getParameter("processNo");

        JSONObject jsonObject1 = loginController.getInfo(code);

        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");

        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);

        redirectAttributes.addAttribute("processNo", processNo);
//        System.out.println(processNo);

        return "redirect:/overRate.html";
    }

}
